<template>
  <div class="user-profile">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span>个人信息</span>
          <el-button type="primary" @click="handleEdit">编辑</el-button>
        </div>
      </template>
      
      <div class="profile-info">
        <div class="info-item">
          <span class="label">用户名：</span>
          <span class="value">{{ userInfo.username }}</span>
        </div>
        <div class="info-item">
          <span class="label">邮箱：</span>
          <span class="value">{{ userInfo.email || '未设置' }}</span>
        </div>
        <div class="info-item">
          <span class="label">手机：</span>
          <span class="value">{{ userInfo.phone || '未设置' }}</span>
        </div>
        <div class="info-item">
          <span class="label">角色：</span>
          <span class="value">{{ userInfo.role === 1 ? '管理员' : '普通用户' }}</span>
        </div>
        <div class="info-item">
          <span class="label">注册时间：</span>
          <span class="value">{{ formatDate(userInfo.createTime) }}</span>
        </div>
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      title="编辑个人信息"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          确定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { formatDate } from '@/utils'

const userInfo = ref({})
const dialogVisible = ref(false)
const loading = ref(false)
const form = ref({
  email: '',
  phone: ''
})

const rules = {
  email: [
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ]
}

const getUserInfo = async () => {
  try {
    const res = await request.get('/api/user/info')
    console.log('获取用户信息成功:', res)
    userInfo.value = res.data
  } catch (error) {
    console.error('获取用户信息失败:', error)
    if (error.response?.status === 401) {
      ElMessage.warning('请先登录')
    } else {
      ElMessage.error('获取用户信息失败')
    }
  }
}

const handleEdit = () => {
  form.value = {
    email: userInfo.value.email,
    phone: userInfo.value.phone
  }
  dialogVisible.value = true
}

const handleSubmit = async () => {
  loading.value = true
  try {
    await request.put('/api/user/profile', {
      ...userInfo.value,
      ...form.value
    })
    ElMessage.success('更新成功')
    dialogVisible.value = false
    getUserInfo()
  } catch (error) {
    console.error('更新失败:', error)
    if (error.response?.status === 401) {
      ElMessage.warning('请先登录')
    } else {
      ElMessage.error('更新失败')
    }
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getUserInfo()
})
</script>

<style lang="scss" scoped>
.user-profile {
  padding: 20px;
  
  .profile-card {
    max-width: 800px;
    margin: 0 auto;
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .profile-info {
      .info-item {
        margin-bottom: 15px;
        
        .label {
          color: #606266;
          margin-right: 10px;
        }
        
        .value {
          color: #303133;
          font-weight: 500;
        }
      }
    }
  }
}
</style> 